package com.chinapost.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.chinapost.dao.BaseDaoI;
import com.chinapost.po.Tmenu;
import com.chinapost.po.Tresource;
import com.chinapost.po.Trole;
import com.chinapost.service.RoleServiceI;
import com.chinapost.vo.DataGrid;
import com.chinapost.vo.Role;

@Service("roleService")
public class RoleServiceImpl implements RoleServiceI {

	private BaseDaoI<Trole> roleDao;
	private BaseDaoI<Tresource> resourceDao;
	private BaseDaoI<Tmenu> menuDao;
	
	public BaseDaoI<Trole> getRoleDao() {
		return roleDao;
	}

	@Autowired
	public void setRoleDao(BaseDaoI<Trole> roleDao) {
		this.roleDao = roleDao;
	}

	public BaseDaoI<Tresource> getResourceDao() {
		return resourceDao;
	}

	@Autowired
	public void setResourceDao(BaseDaoI<Tresource> resourceDao) {
		this.resourceDao = resourceDao;
	}

	public BaseDaoI<Tmenu> getMenuDao() {
		return menuDao;
	}

	@Autowired
	public void setMenuDao(BaseDaoI<Tmenu> menuDao) {
		this.menuDao = menuDao;
	}


	@Override
	public DataGrid datagrid(Role role) {
		DataGrid j = new DataGrid();
		j.setRows(changeModel(find(role)));
		j.setTotal(total(role));
		return j;
	}

	private List<Role> changeModel(List<Trole> troles) {
		List<Role> roles = new ArrayList<Role>();
		if (troles != null && troles.size() > 0) {
			for (Trole tu : troles) {
				Role u = new Role();
				BeanUtils.copyProperties(tu, u);
				Set<Tresource> tresources = tu.getTresources();
				String resourceIds = "";
				String resourceNames = "";
				boolean b = false;
				if (tresources != null && tresources.size() > 0) {
					for (Tresource tresource : tresources) {
							if (b) {
								resourceIds += ",";
								resourceNames += ",";
							}
							resourceIds += tresource.getCid();
							resourceNames += tresource.getCname();
							b = true;
					}
				}
				Set<Tmenu> tmenus = tu.getTmenus();
				String menuIds = "";
				String menuNames = "";
				boolean f = false;
				if (tmenus != null && tmenus.size() > 0) {
					for (Tmenu tmenu : tmenus) {
							if (f) {
								menuIds += ",";
								menuNames += ",";
							}
							menuIds += tmenu.getCid();
							menuNames += tmenu.getCname();
							f = true;
					}
				}
				u.setResourceIds(resourceIds);
				u.setResourceNames(resourceNames);
				u.setMenuIds(menuIds);
				u.setMenuNames(menuNames);
				roles.add(u);
			}
		}
		return roles;
	}

	private List<Trole> find(Role role) {
		String hql = "from Trole t where 1=1 ";
		if (role.getSort() != null && role.getOrder() != null) {
			hql += " order by " + role.getSort() + " " + role.getOrder();
		}
		List<Object> values = new ArrayList<Object>();
		hql = addWhere(role, hql, values);
		return roleDao.find(hql, values, role.getPage(), role.getRows());
	}

	private Long total(Role role) {
		String hql = "select count(*) from Trole t where 1=1 ";
		List<Object> values = new ArrayList<Object>();
		hql = addWhere(role, hql, values);
		return roleDao.count(hql, values);
	}

	private String addWhere(Role role, String hql, List<Object> values) {
		return hql;
	}

	@Override
	public void edit(Role role) {
		Trole r = roleDao.get(Trole.class, role.getCid());
		BeanUtils.copyProperties(role, r, new String[] { "cid" });
		this.saveRoleMenu(role, r);
		this.saveRoleResource(role, r);

	}

	@Override
	public void delete(String ids) {
		if (ids != null) {
			for (String id : ids.split(",")) {
				Trole r = roleDao.get(Trole.class, id.trim());
					roleDao.delete(r);
				}
			}
		}

	@Override
	public List<Role> combobox() {
		List<Role> rl = new ArrayList<Role>();
		List<Trole> l = roleDao.find("from Trole");
		if (l != null && l.size() > 0) {
			for (Trole t : l) {
				Role r = new Role();
				r.setCid(t.getCid());
				r.setCname(t.getCname());
				rl.add(r);
			}
		}
		return rl;
	}

	@Override
	public void add(Role role) {
		Trole r = new Trole();
		BeanUtils.copyProperties(role, r);
		r.setCid(UUID.randomUUID().toString());
		roleDao.save(r);
		this.saveRoleResource(role, r);
		this.saveRoleMenu(role, r);
	}

	private void saveRoleResource(Role role, Trole r) {
		r.setTresources(null);
		Set<Tresource> tresources = new HashSet<Tresource>();
		if (role.getResourceIds() != null) {
			for (String id : role.getResourceIds().split(",")) {
				tresources.add(resourceDao.get(Tresource.class, id.trim()));
			}
			r.setTresources(tresources);
		}
	}

	private void saveRoleMenu(Role role, Trole r) {
		r.setTmenus(null);
		Set<Tmenu> tmenus = new HashSet<Tmenu>();
		if (role.getMenuIds() != null) {
			for (String id : role.getMenuIds().split(",")) {
				tmenus.add(menuDao.get(Tmenu.class,id.trim()));
			}
			r.setTmenus(tmenus);
		}
	}


}
